package gui;

import guiCode.blockAccessCode;

import java.awt.Color;
import java.awt.Rectangle;


import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.JCheckBox;

import secretary.defineSemester;

import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import javax.swing.JScrollPane;



public class BlockAccessGUI extends JPanel{
	
		
	    public JButton btnBack;
		private JLabel lblStudentName ;
	
		private JComboBox studentNameComboBox;
		private blockAccessCode bac;
		private CachedRowSetImpl crs;

		private String firstParent;
		private String secondParent;
		private JCheckBox checkBoxFirstParent;
		private JCheckBox checkBoxSecondParent;
		private JLabel lblParentBlockStatus;
		private JLabel lblBlock;
		private JLabel lblParentName;
		public defineSemester semester=new defineSemester();
		public JPanel blockStatusPanel;
		private JScrollPane scrollPane;
		private JButton btnShow;
  
	public BlockAccessGUI(int x, int y, ClientConsole connection) {
		super();
		setForeground(SystemColor.desktop);
		setBackground(new Color(204, 255, 255));
		
		this.setSize(450, 390);
		//this.setLocation(200,200);
		
		bac=new blockAccessCode(connection);
		bac.setCurrentSemester(semester.identify(0, connection, crs));
		initialize(360, 329);
		
	}
	
	
	/**
	 * Query the data base for All student exists 
	 * @return studentNameComboBox student ComboBox Component
	 */
	public JComboBox getStudentNameComboBox() 
	{
		/*Make query #62(Get all Student form student Table) when click on ComboBox*/
		
		studentNameComboBox = new JComboBox(bac.getStudent());
		studentNameComboBox.setForeground(SystemColor.desktop);
		studentNameComboBox.setBackground(SystemColor.activeCaption);
		studentNameComboBox.setMaximumRowCount(4);
		studentNameComboBox.setBounds(133, 67, 217, 20);
		studentNameComboBox.setSelectedIndex(0);
		
		
		/*studentNameComboBox.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				studentComboboxResults(cb);
				
			}	
		});*/
		return studentNameComboBox;
	}
	
	
	private void initialize(int high,int width) {
		
		this.setSize(360,367);
		this.setLayout(null);
		
		btnShow = new JButton("Show");
		btnShow.setForeground(SystemColor.desktop);
		btnShow.setBackground(SystemColor.activeCaption);
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!studentNameComboBox.getSelectedItem().equals("Select...")){
					
					studentComboboxResults(studentNameComboBox);
					blockStatusPanel.setVisible(true);
				}
				else
					blockStatusPanel.setVisible(false);
			}
		});
		
		
		btnShow.setBounds(261, 117, 89, 23);
		add(btnShow);
		
		blockStatusPanel = new JPanel();
		blockStatusPanel.setBackground(new Color(102, 255, 255));
		blockStatusPanel.setBounds(37, 187, 313, 121);
		add(blockStatusPanel);
		blockStatusPanel.setLayout(null);
		
		lblParentBlockStatus = new JLabel("Parent block status: ");
		lblParentBlockStatus.setBackground(new Color(102, 255, 255));
		lblParentBlockStatus.setForeground(Color.BLUE);
		lblParentBlockStatus.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblParentBlockStatus.setBounds(0, 0, 255, 33);
		blockStatusPanel.add(lblParentBlockStatus);
		
		lblBlock = new JLabel("IsBlocked?");
		lblBlock.setBackground(new Color(153, 255, 153));
		lblBlock.setBounds(0, 44, 69, 14);
		blockStatusPanel.add(lblBlock);
		lblBlock.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		lblParentName = new JLabel("Parent name and ID :");
		lblParentName.setBackground(new Color(153, 255, 153));
		lblParentName.setBounds(99, 44, 156, 14);
		blockStatusPanel.add(lblParentName);
		lblParentName.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		checkBoxFirstParent = new JCheckBox("");
		checkBoxFirstParent.setBackground(new Color(102, 255, 255));
		checkBoxFirstParent.setBounds(0, 65, 271, 23);
		blockStatusPanel.add(checkBoxFirstParent);
		checkBoxFirstParent.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent e) {
				
					if(!checkBoxFirstParent.isSelected()){
					
					bac.setBlockStatus(false, bac.getStudentID(firstParent));
					checkBoxFirstParent.setForeground(Color.GREEN);
					JOptionPane.showMessageDialog(null,"Parent ID# "+firstParent + " was Unblocked");
				}
				else{
					
					bac.setBlockStatus(true,bac.getStudentID(firstParent));
					checkBoxFirstParent.setForeground(Color.RED);
					JOptionPane.showMessageDialog(null,"Parent ID# "+firstParent + " was Blocked");
				  } 
				
			}
		});
		
		
		checkBoxSecondParent = new JCheckBox("");
		checkBoxSecondParent.setBackground(new Color(102, 255, 255));
		checkBoxSecondParent.setBounds(0, 91, 271, 23);
		blockStatusPanel.add(checkBoxSecondParent);
		checkBoxSecondParent.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				
					if(!checkBoxSecondParent.isSelected()){
					
					bac.setBlockStatus(false,bac.getStudentID(secondParent));
					checkBoxSecondParent.setForeground(Color.GREEN);
					JOptionPane.showMessageDialog(null,"Parent ID# "+secondParent + " was Unblocked");
				}
				else{
					
					bac.setBlockStatus(true, bac.getStudentID(secondParent));
					checkBoxSecondParent.setForeground(Color.RED);
					JOptionPane.showMessageDialog(null,"Parent ID# "+secondParent + " was Blocked");
				  } 
				
			}
		});
		
		blockStatusPanel.setVisible(false);
			
		
		
		lblStudentName = new JLabel();
		lblStudentName.setBounds(new Rectangle(10, 68, 113, 19));
		lblStudentName.setText("Student Name : ");
		this.add(lblStudentName, null);
				
		this.add(getStudentNameComboBox(), null);
	    
		JLabel lblChooseStudentYou = new JLabel("Choose student you wish block parent access to : ");
		lblChooseStudentYou.setBounds(10, 27, 313, 44);
		add(lblChooseStudentYou);
		
		btnBack = new JButton("Back");
		btnBack.setForeground(SystemColor.desktop);
		btnBack.setBackground(SystemColor.activeCaption);
		btnBack.setBounds(0, 0, 75, 32);
		add(btnBack);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(37, 89, 2, 2);
		add(scrollPane);
	}
	
	
	private void studentComboboxResults(JComboBox cb){
		 String s;
		s = (String)cb.getSelectedItem();
		
		/*************************************/
		System.out.print(s+"\n");
		/**************************************/
		crs=bac.getParentDetails(bac.getStudentID(s));
		for(int i=0;i<crs.size();i++){
			
		try {
			crs.next();
			if(crs.getString("isBlocked").equals("1") ){
				if(i==0){
					
					checkBoxFirstParent.setSelected(true);
					checkBoxFirstParent.setText("                    " + crs.getString("ParentID")+", "+crs.getString("parentFirstName")+"  "+crs.getString("parentLastName"));
					checkBoxSecondParent.setVisible(false);          //		when only one parent available for the student.dont show the second checkBox. 
					checkBoxFirstParent.setForeground(Color.RED);
					firstParent = crs.getString("ParentID").concat(" ,"+crs.getString("parentFirstName")).concat(" " + crs.getString("parentLastName"));
					}
				if(i==1){
					checkBoxSecondParent.setSelected(true);
					checkBoxSecondParent.setText("                    " + crs.getString("ParentID")+", "+crs.getString("parentFirstName")+"  "+crs.getString("parentLastName"));
					checkBoxSecondParent.setForeground(Color.RED);
					checkBoxSecondParent.setVisible(true);
				    secondParent = crs.getString("ParentID").concat(" ,"+crs.getString("parentFirstName")).concat(" " + crs.getString("parentLastName"));
				}
				
				
			}
			else if(crs.getString("isBlocked").equals("0") ){
				if(i==0){
					checkBoxFirstParent.setSelected(false);
					checkBoxFirstParent.setText("                    " + crs.getString("ParentID")+", "+crs.getString("parentFirstName")+"  "+crs.getString("parentLastName"));
					checkBoxSecondParent.setVisible(false);			//		when only one parent available for the student.dont show the second checkBox.
					checkBoxFirstParent.setForeground(Color.GREEN);
					firstParent = crs.getString("ParentID").concat(" ,"+crs.getString("parentFirstName")).concat(" " + crs.getString("parentLastName"));
				}
				if(i==1){
					checkBoxSecondParent.setSelected(false);
					checkBoxSecondParent.setText("                    " + crs.getString("ParentID")+", "+crs.getString("parentFirstName")+"  "+crs.getString("parentLastName"));
					checkBoxSecondParent.setForeground(Color.GREEN);
					checkBoxSecondParent.setVisible(true);
				     secondParent = crs.getString("ParentID").concat(" ,"+crs.getString("parentFirstName")).concat(" " + crs.getString("parentLastName"));
				}
		    }
			
			
		} catch (SQLException e1) {
			e1.printStackTrace();}
	}
	blockStatusPanel.setVisible(true);
		
	}
}
